package demo.web;

import demo.vo.common.ApiResult;
import io.prometheus.client.Counter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@RestController
@RequestMapping(path = "/redis")
public class RedisController {

    private static final Logger logger = LoggerFactory.getLogger(RedisController.class);
    Random random = new Random();

    private static final Counter requestTotal = Counter.build()
            .name("pingMethodConter").help("pingMethodConter").register();


    //    @Autowired
//    @Qualifier("jedisStringTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    Jedis jedis;

    @GetMapping("/")
    public ApiResult<String> index(HttpServletRequest request) {
        return new ApiResult<>("WELCOME");
    }

    @GetMapping("/get")
    public ApiResult<String> testRedis(HttpServletRequest request, @RequestParam("key") String key) {
        requestTotal.inc();
        String t = jedis.get(key);
        return new ApiResult<>(t);
    }


}
